package br.unb.graphics.qos;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.text.TextAction;

import br.unb.core.dispatcher.TrafficDispatcher;
import br.unb.graphics.InterfaceFactory;
import br.unb.utils.Console;
import br.unb.utils.Resource;

/**
 *
 * @author spider
 */
public class QosPanel extends JPanel {

    private QoSController qosController;
    //images
    public static String CHART = "chart";
    public static String BLANK = "blank";
    public static String CHECK = "check";
    //menuitens
    public static String DELAY = "delay";
    public static String JITTER = "jitter";
    public static String PACKET_LOSS = "packetLoss";
    public static String LOSS = "loss";
    public static String BANDWIDTH = "bandwidth";
    public static String WINDOW = "window";
    private JPanel bc;
    private JPanel dc;
    private JPanel jc;
    private JPanel lc;
    private JTabbedPane tabs;
    private InterfaceFactory ifactory;
    //private boolean hasDelay, hasJitter, hasLoss, hasBandwidth;

    public QosPanel() {
        super(new BorderLayout());
        qosController = TrafficDispatcher.qosController;
        ifactory = new InterfaceFactory(Resource.QosPanel, getActions());
        bc = (JPanel) qosController.loadBandwidthChart();
        dc = (JPanel) qosController.loadDelayChart();
        jc = (JPanel) qosController.loadJitterChart();
        lc = (JPanel) qosController.loadLossChart();
        tabs = new JTabbedPane();
        add(BorderLayout.CENTER, tabs);
        tabs.addTab(
                ifactory.getResourceString(BANDWIDTH),
                ifactory.getIcon(CHART),
                bc);
        tabs.addTab(
                ifactory.getResourceString(DELAY),
                ifactory.getIcon(CHART),
                dc);
        tabs.addTab(
                ifactory.getResourceString(JITTER),
                ifactory.getIcon(CHART),
                jc);
        tabs.addTab(
                ifactory.getResourceString(LOSS),
                ifactory.getIcon(CHART),
                lc);

    }

    /**
     * Fetch the list of actions supported by this
     * editor.  It is implemented to return the list
     * of actions supported by the embedded JTextComponent
     * augmented with the actions defined locally.
     */
    public Action[] getActions() {
        return TextAction.augmentList(new JTextArea().getActions(), actions);
    }
    /************************* action implementations *****************************/
    /**
     * Actions defined by the ControlComponent class
     */
    private Action[] actions = {
        new DelayAction(),
        new JitterAction(),
        new LossAction(),
        new BandwidthAction(),
        new WindowAction()};

    /*********************************************************************/
    /*********************         ACTIONS           *********************/
    /*********************************************************************/

    class WindowAction extends AbstractAction {

        WindowAction() {
            super(WINDOW);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            String windowString;
            windowString = JOptionPane.showInputDialog(
                    Resource.getString("TIME_WINDOW"));
            try {
                int window = Integer.parseInt(windowString);
                if (window > 0) {
                    qosController.setWindowSize(window);
                    //if (Path.isGrafficExtension(file)) {
                        /*removeDelayChart();
                    removeJitterChart();
                    removeLossChart();
                    removeBandwidthChart();
                    Runtime.getRuntime().gc();
                     */
                    //  TrafficReceiverDispatcher.qosController.loadValuesFromFile(file);
                    // }
                } else {
                    throw new NumberFormatException("");
                }
            } catch (NumberFormatException nfe) {
                Console.showMessageDialog(
                        Resource.getString("INVALID_WINDOW"));
            }
        }
    }

    class DelayAction extends AbstractAction {

        DelayAction() {
            super(DELAY);
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            //if (!hasDelay) {
            tabs.addTab(
                    ifactory.getResourceString(DELAY),
                    ifactory.getIcon(CHART),
                    dc);
            JMenuItem mi = ifactory.getMenuItem(DELAY);
            mi.setIcon(ifactory.getIcon(CHECK));
            /*    hasDelay = true;
            } else {
            tabs.remove(
            qosController.loadDelayChart());
            qosController.removeDelayChart();
            JMenuItem mi = ifactory.getMenuItem(DELAY);
            mi.setIcon(ifactory.getIcon(BLANK));
            hasDelay = false;
            }*/
        }
    }

    class JitterAction extends AbstractAction {

        JitterAction() {
            super(JITTER);
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            //if (!hasJitter) {
            tabs.addTab(
                    ifactory.getResourceString(JITTER),
                    ifactory.getIcon(CHART),
                    jc);
            JMenuItem mi = ifactory.getMenuItem(JITTER);
            mi.setIcon(ifactory.getIcon(CHECK));
            /*    hasJitter = true;
            } else {
            tabs.remove(
            qosController.loadJitterChart());
            qosController.removeJitterChart();
            JMenuItem mi = ifactory.getMenuItem(JITTER);
            mi.setIcon(ifactory.getIcon(BLANK));
            hasJitter = false;
            }*/
        }
    }

    class LossAction extends AbstractAction {

        LossAction() {
            super(LOSS);
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            //if (!hasLoss) {
            tabs.addTab(
                    ifactory.getResourceString(LOSS),
                    ifactory.getIcon(CHART),
                    lc);
            JMenuItem mi = ifactory.getMenuItem(LOSS);
            mi.setIcon(ifactory.getIcon(CHECK));
            /*    hasLoss = true;
            } else {
            tabs.remove(
            qosController.loadLossChart());
            qosController.removeLossChart();
            JMenuItem mi = ifactory.getMenuItem(LOSS);
            mi.setIcon(ifactory.getIcon(BLANK));
            hasLoss = false;
            }*/
        }
    }

    class BandwidthAction extends AbstractAction {

        BandwidthAction() {
            super(BANDWIDTH);
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            //if (!hasBandwidth) {
            tabs.addTab(
                    ifactory.getResourceString(BANDWIDTH),
                    ifactory.getIcon(CHART),
                    bc);
            JMenuItem mi = ifactory.getMenuItem(BANDWIDTH);
            mi.setIcon(ifactory.getIcon(CHECK));
            /*    hasBandwidth = true;
            } else {
            tabs.remove(
            qosController.loadBandwidthChart());
            qosController.removeBandwidthChart();
            JMenuItem mi = ifactory.getMenuItem(BANDWIDTH);
            mi.setIcon(ifactory.getIcon(BLANK));
            hasBandwidth = false;
            }*/
        }
    }
}
